/*
* @Author: fredlee
* @Date:   2017-04-19 13:27:00
 * @Last Modified by: mikey.zhaopeng
 * @Last Modified time: 2017-12-11 20:25:28
*/

import Vue from 'vue';
import seller from './seller.js';
import memberSeller from './memberSeller.js'
import pay from './pay.js'
import user from './user.js'
import Base64 from 'js/config/base64'
//import goodsList from './goodsList'

const vm = new Vue()

function searchGoodsList (params) {
	return vm.$http.get(vm.$CONSTANTS.APIRecharge + 'publishAliYun/publishSearch', {
		params: params
	});
}

function searchgamelistbytype (params) {
	return vm.$http.post(vm.$CONSTANTS.P30006 + 'category/searchgamelistbytype', params);
}

//获取订单和环信帐号信息：
function GetOrderImInfo (url) {
  return vm.$http.jsonp('//chatregister.5173.com/Easemob/GetOrderImInfo?callurl='+encodeURIComponent(url));
}

export const ChoiceApi = {
	// 获取游戏列表数据
	// 参数格式：
	// {
	// 	pageIndex: 1,
	// 	pageSize: 80,
	// 	isShowInHome: true,
	// 	goodsType: 2
	// }
	searchGameListByType: function searchGameListByType (params) {
		return vm.$http.post(vm.$CONSTANTS.P30006 + 'category/searchgamelistbygoodstype' ,params)
	},
	// 获取游戏首字母列表
	// 参数格式：
	// {
	// 	gameType:1，
	// 	goodsTypeId:2
	// }
	getGamePy: function getGamePy (params) {
		return vm.$http.post(vm.$CONSTANTS.P30006 + 'category/getgamepy' , params)
	},
	// 类型：post
	// 参数：
	// {
	// 	accurateMap: {
	// 		goods_type: [goodsType.toString()],
	// 		game_id: [gameId],
	// 		server_id: [serverId],
	// 		region_id: [areaId]
	// 	},
	// 	keyWordMap: {},
	// 	betweenMap: {},
	// 	page: this.page,
	// 	pageCount: pageCount,
	// 	sortMap: {
	// 		goods_source_type: "+",
	// 		create_time: "-"
	// 	}
	// }
	goodsSearchList: function goodsSearchList(params) {
		return vm.$http.post(vm.$CONSTANTS.P30006 + 'goodsSearch/goodsSearchList', params)
	},
	// 根据游戏区服获取快销号列表：
	// 参数：
	// {aid=a9788cd93b7c4a90b8a0605dd269d806}
	getQuickList: function getgameServer(params) {
		return vm.$http.post(vm.$CONSTANTS.P30005 + 'fast_sale_account/getQuickPublishInfoList', params)
	},
	// 参数：
	// {serverId:serverId}
	convertgoodsurl: function convertgoodsurl (params) {
		return vm.$http.get(vm.$CONSTANTS.APIGoods + 'mainsite/convertgoodsurl', {
			params: params
		})
	},
	// 根据游戏ID获取商品类型：
	// 参数：
	// {gameId:gameId}
	searchGoodsTypeByGameid: function searchGoodsTypeByGameid (params) {
      return vm.$http.post(vm.$CONSTANTS.P30006 + 'category/searchgoodstypebygameid', params)
	},
	// 根据游戏ID获取游戏阵营：
	// 参数：
	// {parentId:YX16060613502502300005}
	searchFactionByGameid: function searchFactionByGameid(params) {
		return vm.$http.get(vm.$CONSTANTS.P30006 + 'mainMobileGame/searchfactionbygameid', {
			params: params
		})
	},

	// 根据游戏ID获取平台：
	// 参数：
	// {gameId:YX17051511021172700019}
	getplatforminfobygameid: function getplatforminfobygameid (params) {
		return vm.$http.get(vm.$CONSTANTS.APICategory + 'mainMobileGame/getplatforminfobygameid', {
			params: params
		})
	},

	// 根据游戏ID,平台ID获取客户端：
	// 参数：
	// {gameId=YX17051511021172700019&platformId=5c3c40536c6e4c359244e1107074e7f4}
	getgameareasbygameandplatform: function getgameareasbygameandplatform(params) {
		return vm.$http.get(vm.$CONSTANTS.P30006 + 'mainMobileGame/getgameareasbygameandplatform', {
			params: params
		})
	},

	// 根据客户端ID获取区服：
	// 参数：
	// {aid=a9788cd93b7c4a90b8a0605dd269d806}
	getgameServer: function getgameServer(params) {
		return vm.$http.get(vm.$CONSTANTS.P30006 + 'mainMobileGame/getgameServer', {
			params: params
		})
	},

	// 端游根据游戏ID获取区：
	// 参数：
	// {parentId=YX16060613502502300005&gid=YX16060613502502300005}
	searchbyparentid: function searchbyparentid (params) {
        return vm.$http.get(vm.$CONSTANTS.P30006 + 'mainMobileGame/getOnlineAreaByGameId', {params:params})
    },

	// 端游根据游戏ID获取服：
	// 参数：
	// {parentId=YX16060613502502300005}
	searchserverbyparentid: function searchserverbyparentid(params) {
        return vm.$http.get(vm.$CONSTANTS.P30006 + 'mainMobileGame/getgameServer', {
            params: params
        })

	},

	// 获取端游筛选：
	// 参数：
	// goodsTypeId=1&gameName=dnf
	queryFilterProperty: function queryFilterProperty(params) {
		return vm.$http.post(vm.$CONSTANTS.P30006 + 'gamePropertyRelation/queryFilterProperty', params)
	},


}

export const AccountApi = {
	// {
	// 	goodsId:xxxx
	// }
	selectGoodsDetail: function selectGoodsDetail (params) {
		return vm.$http.get(vm.$CONSTANTS.APIGoods + 'goods/selectgoodsdetail', {
			params: params
		})
	},
	//获取可领取红包列表
	getRedPaper: function getRedPaper (params) {
		return vm.$http.post(vm.$CONSTANTS.APIGoods + 'coupon/getRuleCouponsNOTLogin',params )
	},
	//获取登录状态可领取红包列表
	getLoginRedPaper: function getRedPaper (params) {
		return vm.$http.post(vm.$CONSTANTS.APIGoods + 'coupon/getRuleCouponsLogin',params )
	},
	//获取可领取红包列表
	receiveCouponToUser: function getRedPaper (params) {
		return vm.$http.post(vm.$CONSTANTS.APIGoods + 'coupon/receiveCouponToUser',params )
	},
	//获取已领取红包列表
	getMyPaper: function getRedPaper (params) {
		return vm.$http.post(vm.$CONSTANTS.APIGoods + 'coupon/queryCouponListByOrder',params )
	},
	// {
	// 	BizOfferId:DB047-20170508-76448621
	// }
	selectPcGoodsDetail: function selectPcGoodsDetail(params) {
		return vm.$http.get(vm.$CONSTANTS.P30005 + 'mainGoods/selectpcgoodsdetail', {
			params: params
		})
	},
	selectPcMobGoodsDetail: function selectPcMobGoodsDetail (params) {
		return vm.$http.post(vm.$CONSTANTS.APIMainGoods + 'mainMobileGoods/getMainGoodsDetail', params)
	},
	selectPcMobGoodsDetailNew (params) {
		return vm.$http.post(vm.$CONSTANTS.APIMGCoin+ 'goods/getGoodsDetail', {bizOfferId: params.goodsId})
	},
	// 参数：
	// {goodsId:this.$route.params.goodsId}
	addBrowseAmount: function addBrowseAmount (params) {
		return vm.$http.post(vm.$CONSTANTS.APIGoods + 'goodsBrowse/addBrowseAmount', params)
	}
}

export const BuyerCenterApi = {
	getCustomerInfo: function getCustomerInfo(params) {
		return vm.$http.get(vm.$CONSTANTS.APIRc + 'IMService/getCustomerInfo', {
			params
		})
	},
	//m站环信配置
	getMorderIm: function getMorderIm(params) {
		return vm.$http.jsonp(vm.$CONSTANTS.APIOrder + 'orderdata/getOrderIm', {
			params
		})
	},
	//获取主站寄售环信配置
	getPcJiIm: function getPcJiIm(params) {
		return vm.$http.jsonp('http://consignment.5173.com/ajax.axd?methodName=GetOrderIMInfo', {
			params
		})
	},
	//获取主站担保环信配置
	getPcDanIm: function getPcDanIm(params) {
		return vm.$http.jsonp('http://danbao.5173.com/ajax.axd?methodName=GetOrderIMInfo', {
			params
		})
	},
	//获取主站账号环信配置
	getPcAccountIm: function getPcAccountIm(params) {
		return vm.$http.jsonp('http://gameid.5173.com/BankInfo.axd?methodName=GetOrderIMInfo', {
			params
		})
	},
	//获取金币商城环信配置
	getGoldOrderIm: function getGoldOrderIm(params) {
		return vm.$http.jsonp('http://yxbmall.5173.com/gamegold-facade-frontend/services/orderdata/getOrderIm', {
			params
		})
	},
	//获取主站手游环信配置
	getOrderIm: function getOrderIm(url) {

		//return vm.$http.jsonp('http://sy.5173.com/api/order/getorderim', {params})---GetOrderImInfo
		return vm.$http.jsonp(location.protocol + '//chatregister.5173.com/Easemob/GetOrderTokenInfo', {
			params: {
				callurl: encodeURI(url)
			}
		})
	},
	//获取订单中心列表
	getOrderList: function getOrderList(params) {
		return vm.$http.jsonp(location.protocol + '//trade.5173.com/getorder.ashx', {
			params: params
		})
	},
	getPcServer: function getPcServer(params) {
		var obj;
		var base11 = new Base64.encode64();
		var str = base11.encode('http://consignment.5173.com/ajax.axd?methodName=' + params.methodName + '&orderid=' + params.orderid + '&usertype=' + params.usertype + '&tradingtype=' + params.tradingtype + '&clienttype=2')
		// console.log(str)


		return vm.$http.get(vm.$CONSTANTS.APIMainGoods+'mainGoods/urlTranfer', {
			params: {
				url: str
			}
		}).then(
			(res) => {
				var resObj = JSON.parse(res.data.result)
				console.log("111111111",resObj)
				if(resObj.Data){
					if(resObj.Data.Value.indexOf('erviceHx')>0){
						obj = {
							serverType:1,
							params:''
						}

					}else if(resObj.Data.Value.indexOf('KF.im_openInPageChat')>0){
						var xiaoneng =  resObj.Data.Value.substring(resObj.Data.Value.indexOf('(')+2,resObj.Data.Value.indexOf(')')-1)
						console.log(xiaoneng)
						obj = {
							serverType:2,
							params:xiaoneng
						}

					}else if(resObj.Data.Value.indexOf('encent')>0){
						var qq = resObj.Data.Value.substring(resObj.Data.Value.indexOf('in=')+3,resObj.Data.Value.indexOf('&Site'))

						obj = {
							serverType:3,
							params:qq
						}
					}
					return obj;
				}
			}
		)
		//   return vm.$http.jsonp('http://consignment.5173.com/ajax.axd', {params:params}).then(
		// 	res=>{
		// 		console.log(res)
		// 		if(resObj){
		// 			if(resObj.Value.indexOf('erviceHx')>0){
		// 				obj = {
		// 					serverType:1,
		// 					params:''
		// 				}

		// 			}else if(resObj.Value.indexOf('KF.im_openInPageChat')>0){
		// 				console.log(resObj.Value.indexOf('('))
		// 				console.log(resObj.Value.indexOf(')'))
		// 				var xiaoneng =  resObj.Value.substring(resObj.Value.indexOf('(')+2,resObj.Value.indexOf(')')-1)
		// 				console.log(xiaoneng)
		// 				obj = {
		// 					serverType:2,
		// 					params:xiaoneng
		// 				}

		// 			}else if(resObj.Value.indexOf('encent')>0){
		// 				var qq = resObj.Value.substring(resObj.Value.indexOf('in=')+3,resObj.Value.indexOf('&Site'))

		// 				obj = {
		// 					serverType:3,
		// 					params:qq
		// 				}
		// 			}
		// 			return obj;
		// 		}


		// 	  }
		//  )
	},
	getImHistory: function getImHistort(params) {
		return vm.$http.jsonp(location.protocol + '//chatrecordService.5173.com/api/ChatRecord', {
			jsonp: 'jsoncallback',
			params
		})
	},
	// {
	// 	status:2, 1是交易中，2是
	//  pageIndex:1,
	//  pageSize:10,
	// }
	getOrderData: function getOrderData (params) {
		return vm.$http.get(vm.$CONSTANTS.APIOrder + 'buyer/order/list', {
			params: params
		})
	},
	getMergeOrderData: function getMergeOrderData (params) {
		return vm.$http.get(vm.$CONSTANTS.APIOrder + 'buyer/order/mergeList', {
			params: params
		}, {
			before(xhr) {
				if (this.LastRequest) {
					this.LastRequest.abort()
				}
				this.LastRequest = xhr
			}
		})
	},
	getOrderDetail: function getOrderDetail (orderId) {
		return vm.$http.get(vm.$CONSTANTS.APIOrder + 'buyer/order/'+orderId)
	},
	queryOrderDetail: function queryOrderDetail (params) {
		return vm.$http.get(vm.$CONSTANTS.APIRecharge + 'buyer/order/queryOrderDetail', {
			params: params
		})
	},
	mainShouYouOrderList: function mainShouYouOrderList (params) {
		return vm.$http.get(vm.$CONSTANTS.APIMainGoods + 'mainShouYouOrderService/mainShouYouOrderList', {
			params: params
		}, {
			before(xhr) {
				if (this.LastRequest) {
					this.LastRequest.abort()
				}
				this.LastRequest = xhr
			}
		})
	},
	mainSYFastOrderList: function mainShouYouOrderList (params) {
		params ={
			orderStatus:params.payStatus,
			pageSize:params.pageSize,
			pageIndex:params.pageIndex,
			syBizOfferType:'6,7'
		}
		return vm.$http.get(vm.$CONSTANTS.APIMainGoods + 'mainFastSalesAccount/getOrderList', {
			params: params
		}, {
			before(xhr) {
				if (this.LastRequest) {
					this.LastRequest.abort()
				}
				this.LastRequest = xhr
			}
		})
	},
	//提取M站账号资料
	getMobAccountInfo: function getMobAccountInfo(params) {
		return vm.$http.get(vm.$CONSTANTS.APIOrder + 'orderdata/data', {
			params: params
		})
	},
	getPcAccountInfo: function getPcAccountInfo(params) {
		return vm.$http.get(vm.$CONSTANTS.APIMainGoods + 'mainGoodsAccountOrder/buyerViewAccountInfo', {
			params: params
		})
	},
	openServiceQQ: function openServiceQQ(params) {
		return vm.$http.post(vm.$CONSTANTS.APIOrder + 'assignCustomServie/openServiceQQ', params)
	},
	//获取pc端商品订单详情
	mainGoodsOrderDetailed: function mainGoodsOrderDetailed(params) {
		return vm.$http.post(vm.$CONSTANTS.APIMainGoods + 'mainGoodsOrder/mainGoodsOrderDetailed', params)
	},
	mainShouYouOrderDetailed: function mainShouYouOrderDetailed (params) {
		return vm.$http.get(vm.$CONSTANTS.APIMainGoods + 'mainShouYouOrderService/mainShouYouOrderDetailed', {
			params: params
		})
	},
	mainShouYouOrderFast (params) {
		return vm.$http.post(vm.$CONSTANTS.APIMGCoin + 'buyer/getOrderDetail', params).then(res => {
			if (res.data.success) {
				_turn(res.data.data)
			}
			return res
		})
		// return vm.$http.get(vm.$CONSTANTS.APIMainGoods + 'mainFastSalesAccount/getOrderDetail', {params:params})
	},
	mainShouYouOrderGetGameAccountInfo (params) {
		return vm.$http.post(vm.$CONSTANTS.APIMGCoin + 'buyer/getGameAccountInfo', params).then(res => {
			if (res.data.success) {
				_turn(res.data.data)
			}
			return res
		})
		// return vm.$http.get(vm.$CONSTANTS.APIMainGoods + 'mainShouYouOrderService/mainShouYouOrderGetGameAccountInfo', {params:params})
	},
	rechargeRefund: function rechargeRefund (params) {
		return vm.$http.post(vm.$CONSTANTS.APIRecharge + 'buyer/order/refund', params)
	},
	appealSure: function appealSure(params) {
		return vm.$http.post(vm.$CONSTANTS.APIRecharge + 'buyer/order/appeal', params)
	},
	//手游充值交易成功申述
	appealReceive: function appealReceive(params) {
		return vm.$http.post(vm.$CONSTANTS.APIRecharge + 'buyer/order/appealReceive', params)
	},
	//主站手游订单付款
	mobileOrderPay: function mobileOrderPay(params) {
		return vm.$http.get(vm.$CONSTANTS.APIMainGoods + 'mainGoodsAccountOrder/mobileOrderPay', {
			params: params
		})
	},
	appealReceive: function appealReceive(params) {
		return vm.$http.post(vm.$CONSTANTS.APIRecharge + 'buyer/order/appealReceive', params)
	},
// {
// 	"jsonStr":{
// 		"OrderType":"buyer",
// 		"Gm":"","Ga":"","Gs":"","RaceId":"","Ts":"","Bt":"","Oc":"1",
// 		"Keyword":"","P":1,"PS":10,"MaxDate":"2017-05-25T08:03:38.996Z","MinDate":"2017-04-24T08:03:38.996Z"
// 	}
// }
	mainGoodsOrderList: function mainGoodsOrderList (params) {
		return vm.$http.post(vm.$CONSTANTS.APIMainGoods + 'mainGoodsOrder/mainGoodsOrderList', params, {
			before(xhr) {
				if (this.LastRequest) {
					this.LastRequest.abort()
				}
				this.LastRequest = xhr
			}
		})
	},

	//M站各状态数量
	buyerMobCount: function buyerMobCount() {
		return vm.$http.get(vm.$CONSTANTS.APIOrder + 'buyer/order/orderCount')
	},
	//PC端各状态数量
	buyerPcCount: function buyerPcCount() {
		return vm.$http.get(vm.$CONSTANTS.APIMainGoods + 'mainShouYouOrderService/mainOrderEveryStatusCount')
	},
	//主站手游订单数量
	buyerShouyouCount: function buyerShouyouCount() {
		return vm.$http.get(vm.$CONSTANTS.APIMainGoods + 'mainShouYouOrderService/mainShouYouOrderEveryStatusCount')
	},
	//商城订单数量
	buyerMallCount: function buyerMallCount() {
		return vm.$http.get(vm.$CONSTANTS.APIGoods + 'purchaseData/myBuyOrderStatusCount')
	},
	//取消订单
	cancelOrder: function cancelOrder(params) {
		return vm.$http.post(vm.$CONSTANTS.APIOrder + 'buyer/order/cancel', params)
	},
	//手游支付
	maypay: function maypay(params) {
		return vm.$http.get(vm.$CONSTANTS.APIOrder + 'payment/mypay', {
			params: params
		})
	},
	//获取PC图片
	getAccountOrderImgInfo: function getAccountOrderImgInfo(params) {
		return vm.$http.get(vm.$CONSTANTS.APIMainGoods + 'mainGoodsAccountOrder/getAccountOrderImgInfo', {
			params: params
		})
	},
	//主站接口获取申请退款按钮
	getAccountBuyAnXin:function getAccountBuyAnXin(params) {
		return vm.$http.get(vm.$CONSTANTS.APISafebuy + 'SafeBuyOrderService/getComplainStatus', {
			params: params
		})
	},
	// lol皮肤接口https转http
	urlTranfer:function urlTranfer(params){
		var obj;
		var base11 = new Base64.encode64();
		var str = base11.encode('http://gameid.5173.com/a.axd?methodName=' + params.methodName + '&id=' + params.id)
		return vm.$http.get(vm.$CONSTANTS.APIMainGoods + 'mainGoods/urlTranfer', {
			params: {
				url: str
			}
		})
	},
	// 获取gtr验证信息
	NoInfoBinding:function NoInfoBinding(params){
		var base11 = new Base64.encode64();
		var str = base11.encode('http://gameid.5173.com/a.axd?methodName=' + params.methodName + '&type='+params.type+'&id=' + params.id)
		return vm.$http.get(vm.$CONSTANTS.APIMainGoods + 'mainGoods/urlTranfer', {
			params: {
				url: str
			}
		})
	},
	// 订单确认购买
	isTrueBuyOrderId:function isTrueBuyOrderId(params){
		var base11 = new Base64.encode64();
		var str = base11.encode('http://gameid.5173.com/a.axd?methodName=' + params.methodName +'&OrderId=' + params.OrderId)
		return vm.$http.get(vm.$CONSTANTS.APIMainGoods + 'mainGoods/urlTranfer', {
			params: {
				url: str
			}
		})
	}


}

// 临时方法：对象字段名首字母小写转首字母大写
function _turn (element) {
	if (element === null || element.constructor === String || element.constructor === Number) {
		// 字符串暂不处理
	} else if (element.constructor === Array) {
		// 数组处理
		element.forEach( v => {
			_turn(v)
		})
	} else {
		// 对象处理
		for (let key in element) {
    		element[key.replace(/^\S/, s => s.toUpperCase())] = element[key]
    		 _turn(element[key])
    		delete element[key]
  		}
	}
}


export default {
	searchGoodsList,
	searchgamelistbytype,
  	GetOrderImInfo,//GetOrderImInfo
	ChoiceApi,
	AccountApi,
	BuyerCenterApi,
	seller,
	memberSeller,
	pay,
	user,
};
